package com.jr.controller;


import com.jr.entity.Job;
import com.jr.entity.User;
import com.jr.entity.UserJobEnterprise;
import com.jr.mapper.UserJobEnterpriseDao;
import com.jr.service.JobService;
import com.jr.service.UserJobEnterpriseService;
import com.jr.util.PageHelper;
import com.jr.util.Sendsms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Commit;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户工作企业三表关联
 *
 * @author Snoopy
 * @since 1.0.0 2022-12-31
 */
@RestController
@SuppressWarnings("all")

public class UserJobEnterpriseController {

    @Autowired
    private UserJobEnterpriseService userJobEnterpriseService;
    @Autowired
    private JobService jobService;

    /**
     * @description: 我的职位展示 分页 模糊
     * @author: 郑佳奇
     * @date: 2023/1/7 8:35
     * @param: [uid, keyword, indexpage]
     * @return: com.jr.util.PageHelper
     **/
    @GetMapping(value = "myjobpageloads",produces = "application/json;charset=utf-8")
    public @ResponseBody PageHelper EnquireAboutJob(String uid,String keyword,int indexpage) throws ParseException {

        PageHelper pageHelper = new PageHelper();
        Map map = new HashMap<String,String>();
        map.put("uid",uid);
        map.put("keyword",keyword);
        pageHelper.setCountnum(userJobEnterpriseService.queryCoountPageById(map));
        pageHelper.setCountpage(pageHelper.getCountnum()%pageHelper.getShowcount()==0?pageHelper.getCountnum()/pageHelper.getShowcount():pageHelper.getCountnum()/pageHelper.getShowcount()+1);
        pageHelper.setIndexpage(indexpage);
        map.put("indexpage",(pageHelper.getIndexpage()-1)*pageHelper.getShowcount());
        map.put("showcount",pageHelper.getShowcount());
        List<UserJobEnterprise> list = userJobEnterpriseService.queryJobAllByUid(map);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        for (UserJobEnterprise userJobEnterprise : list) {
            userJobEnterprise.getJob().setJrefreshdatestr(simpleDateFormat.format(userJobEnterprise.getJob().getJrefreshdate()));
        }

        pageHelper.setIndexinfos(list);

        return pageHelper;
    }

    /**
     * @description: 修改关注态
     * @author: 郑佳奇
     * @date: 2023/1/7 8:34
     * @param: [userJobEnterprise]
     * @return: void
     **/
    @PostMapping(value = "changeattentionstate")
    public void changAettention(UserJobEnterprise userJobEnterprise){
        if (userJobEnterpriseService.selectUIdEidJid(userJobEnterprise)){
            userJobEnterpriseService.modifyEnterpriseAttention(userJobEnterprise);
        }else {
            if(userJobEnterprise.getJobuserattention()==null){
                userJobEnterprise.setJobuserattention("0");
            }else {
                userJobEnterprise.setJobuserstatus("0");
            }
            userJobEnterpriseService.addUIdEidJid(userJobEnterprise);
        }
    }

    /**
     * @description:修改申请状态
     * @author: 李泽涛
     * @date: 2023/1/9 9:05
     * @param: [userJobEnterprise]
     * @return: void
     **/
    @GetMapping(value = "updateStatus",produces = "application/json;charset=utf-8")
    public void updateEnterpriseStatus(UserJobEnterprise userJobEnterprise){
        //判断三连表中有无数据
        if (userJobEnterpriseService.selectUIdEidJid(userJobEnterprise)){
            //如果有这条数据就对申请状态进行修改
            userJobEnterpriseService.modifyEnterpriseStatus(userJobEnterprise);
        }else {
            //如果没有在表中添加这条数据
            if(userJobEnterprise.getJobuserattention()==null){
                userJobEnterprise.setJobuserattention("0");
            }else {
                userJobEnterprise.setJobuserstatus("0");
            }
            userJobEnterpriseService.addUIdEidJid(userJobEnterprise);
        }
    }

    /**
     * @description:搜索页面所有工作的展示
     * @author: 李泽涛
     * @date: 2023/1/9 8:58
     * @param: [indexpage, uid, keyword]
     * @return: com.jr.util.PageHelper
     **/
    @GetMapping(value = "getAllJobs",produces = "application/json;charset=utf-8")
    public  @ResponseBody PageHelper selectAllJobsByUid(Integer indexpage,String uid,String keyword){
        PageHelper pageHelper=new PageHelper();
        Map map=new HashMap<String,String>();
        map.put("uid",uid);
        map.put("keyword",keyword);
        if(indexpage!=null){
            pageHelper.setIndexpage(indexpage);
        }
        pageHelper.setCountnum(userJobEnterpriseService.getCountNum(map));//总条数
        int countpage=pageHelper.getCountnum()%pageHelper.getShowcount()==0?
                pageHelper.getCountnum()/pageHelper.getShowcount():
                pageHelper.getCountnum()/pageHelper.getShowcount()+1;
        pageHelper.setCountpage(countpage);//总页数
        pageHelper.setIndexpage(indexpage);
        map.put("indexpage",(pageHelper.getIndexpage()-1)*pageHelper.getShowcount());
        map.put("showcount",pageHelper.getShowcount());
        List<UserJobEnterprise> alljobs=userJobEnterpriseService.queryJobAll(map);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (UserJobEnterprise userJobEnterprise:alljobs){
            userJobEnterprise.getJob().setJrefreshdatestr(simpleDateFormat.format(userJobEnterprise.getJob().getJrefreshdate()));
        }
        pageHelper.setIndexinfos(alljobs);
        return pageHelper;
    }

}